{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.de54e4ebd852752e0606811615114e75.315360000.1918819076.282335-22838595', 'expires_in': 2592000, 'session_key': '9mzdXUbOcgipXDz8U88KTdlM79deN/rPZKvZc7zZUNS+elFKGjBgm9G0+HGHvNwsx0Dswo9tosumAfQphK8a1DizZ2iXng==', 'access_token': '24.f2fa6a48e0a6e8b756bd662f46c89ef2.2592000.1606051076.282335-22838595', 'scope': 'public brain_all_scope vis-faceverify_faceverify_h5-face-liveness vis-faceverify_FACE_V3 vis-faceverify_idl_face_merge vis-faceverify_FACE_EFFECT wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component', 'session_secret': 'cda7d24f2b96bbca02454fd5c6cce694'}\n"
     ]
    }
   ],
   "source": [
    "# client_id 为官网获取的API Key， client_secret 为官网获取的Secret Key\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}'\n",
    "client_id = \"DlnpohsOvQe6j7q2yaXi4k5n\"\n",
    "client_secret = \"wQm2WIij5jy7uAl6rxbHeScmIt8kS5ma\"\n",
    "response = requests.get(host.format(client_id, client_secret))\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 2599890579153,\n",
       " 'timestamp': 1603459076,\n",
       " 'cached': 0,\n",
       " 'result': {'face_num': 1,\n",
       "  'face_list': [{'face_token': 'c7029f73038bb572d1db4789088f85f5',\n",
       "    'location': {'left': 73.58,\n",
       "     'top': 76.94,\n",
       "     'width': 38,\n",
       "     'height': 40,\n",
       "     'rotation': -3},\n",
       "    'face_probability': 1,\n",
       "    'angle': {'yaw': -18.12, 'pitch': 10.44, 'roll': -4.81},\n",
       "    'face_shape': {'type': 'round', 'probability': 0.45},\n",
       "    'face_type': {'type': 'human', 'probability': 0.99}}]}}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.人脸检测与属性分析\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    "\n",
    "params = \"{\\\"image\\\":\\\"https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1354941954,4086942311&fm=26&gp=0.jpg\\\",\\\"image_type\\\":\\\"URL\\\",\\\"face_field\\\":\\\"faceshape,facetype\\\"}\"\n",
    "\n",
    "access_token = '24.6d684a22feb8384590448aa0f4edcb8e.2592000.1605596154.282335-22838595' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 9965356579652,\n",
       " 'timestamp': 1603459108,\n",
       " 'cached': 0,\n",
       " 'result': {'score': 94.37832642,\n",
       "  'face_list': [{'face_token': '91cf0a5aa45b0371989e56760b30548c'},\n",
       "   {'face_token': '93d2501e1cb1685c26107d0b19cc854a'}]}}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2.人脸对比\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/match\"\n",
    "\n",
    "params = \"[{\\\"image\\\": \\\"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3430692674,459091344&fm=26&gp=0.jpg\\\", \\\"image_type\\\": \\\"URL\\\", \\\"face_type\\\": \\\"CERT\\\", \\\"quality_control\\\": \\\"LOW\\\"}, {\\\"image\\\": \\\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603015508446&di=eee4e2c852d804bc3b80a719df3df9ef&imgtype=0&src=http%3A%2F%2Fimg2-cloud.itouchtv.cn%2Ftvtouchtv%2Fimage%2F20170914%2F1505363583630378.jpg\\\", \\\"image_type\\\": \\\"URL\\\", \\\"face_type\\\": \\\"LIVE\\\", \\\"quality_control\\\": \\\"LOW\\\"}]\"\n",
    "# face_type 人脸的类型 LIVE；IDCARD；WATERMARK；CERT；INFRARED。\n",
    "\n",
    "access_token = '24.6d684a22feb8384590448aa0f4edcb8e.2592000.1605596154.282335-22838595' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 223101,\n",
       " 'error_msg': 'group already exist',\n",
       " 'log_id': 9910100135201,\n",
       " 'timestamp': 1603459122,\n",
       " 'cached': 0,\n",
       " 'result': None}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3.创建用户组\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/faceset/group/add\"\n",
    "\n",
    "params = \"{\\\"group_id\\\":\\\"group2\\\"}\"\n",
    "access_token = '24.6d684a22feb8384590448aa0f4edcb8e.2592000.1605596154.282335-22838595' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 1575991011017,\n",
       " 'timestamp': 1603459132,\n",
       " 'cached': 0,\n",
       " 'result': {'face_token': '91cf0a5aa45b0371989e56760b30548c',\n",
       "  'location': {'left': 186.93,\n",
       "   'top': 99.21,\n",
       "   'width': 121,\n",
       "   'height': 118,\n",
       "   'rotation': 1}}}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4.人脸注册\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add\"\n",
    "\n",
    "params = \"{\\\"image\\\":\\\"91cf0a5aa45b0371989e56760b30548c\\\",\\\"image_type\\\":\\\"FACE_TOKEN\\\",\\\"group_id\\\":\\\"group1\\\",\\\"user_id\\\":\\\"user1\\\",\\\"user_info\\\":\\\"abc\\\",\\\"quality_control\\\":\\\"LOW\\\",\\\"liveness_control\\\":\\\"NORMAL\\\"}\"\n",
    "access_token = '24.6d684a22feb8384590448aa0f4edcb8e.2592000.1605596154.282335-22838595' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 1589558935897,\n",
       " 'timestamp': 1603459143,\n",
       " 'cached': 0,\n",
       " 'result': {'face_list': [{'face_token': '91cf0a5aa45b0371989e56760b30548c',\n",
       "    'ctime': '2020-10-23 17:46:47'}]}}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 5.获取用户人脸列表\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/faceset/face/getlist\"\n",
    "\n",
    "params = \"{\\\"user_id\\\":\\\"user1\\\",\\\"group_id\\\":\\\"group2\\\"}\"\n",
    "access_token = '24.6d684a22feb8384590448aa0f4edcb8e.2592000.1605596154.282335-22838595' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 525845594995,\n",
       " 'timestamp': 1603459154,\n",
       " 'cached': 0,\n",
       " 'result': None}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 6.删除用户组\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/faceset/group/delete\"\n",
    "\n",
    "params = \"{\\\"group_id\\\":\\\"group1\\\"}\"\n",
    "access_token = '24.6d684a22feb8384590448aa0f4edcb8e.2592000.1605596154.282335-22838595' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
